# @Time : 2020/7/30 14:54
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np

"""
retval = cv.fitEllipse(points)
返回值retval是RotatedRect类型的值.这是因为该函数返回的是拟合椭圆的外接矩形,retval包含外接矩形的质心,宽,高,旋转角度
等参数信息,这些信息正好与椭圆的中心点,轴长度,旋转角度等信息吻合.
参数points是轮廓.
"""

img = cv.imread("cc.bmp")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

T, thres = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
cv.imshow("Thres", thres)

image, contours, hierarchy = cv.findContours(thres, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
ellipse = cv.fitEllipse(contours[0])
print("Ellipse = {}".format(ellipse))
cv.ellipse(img, ellipse, (0, 255, 0), thickness=2)

cv.imshow("Ellipse",img)
cv.waitKey(0)
